﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Xml;
using Palsta.Core.data;

namespace Palsta.Core.parser
{
    class DataDirectoryParser
    {
        private XmlDocument data = null;
        private XmlDocument schema = null;
        private DataDictionary dataDictionary;


        public DataDictionary Parse(String dataPath, String schemaPath)
        {
            this.dataDictionary = new DataDictionary();
            schema = new XmlDocument();
            schema.Load(schemaPath);
            IEnumerator ie = this.schema.GetEnumerator();
            while (ie.MoveNext())
            {
                XmlNode n = (XmlNode)ie.Current;
                if (n.Name.Equals("xsd:schema"))
                {
                    ie = n.GetEnumerator();
                    break;
                }
            }
            while (ie.MoveNext())
            {
                XmlNode n = (XmlNode)ie.Current;
                this.ParseElement(n);
            }

            data = new XmlDocument();
            data.Load(dataPath);
            XmlNode gen = data.FirstChild;
            ie = gen.GetEnumerator();
            while (ie.MoveNext())
            {
                XmlNode n = (XmlNode)ie.Current;
                this.ParseValue(n);
            }

            return this.dataDictionary;
        }


        private bool ParseElement(XmlNode node)
        {
            String name = InfoPathXml.GetNodeAttr(node, "name");
            if (name == null)
            {
                String refer = InfoPathXml.GetNodeAttr(node, "ref");
                if (refer == null)
                {
                    return false;
                }

            }
            else
            {
                name = "my:" + name;
                String type = InfoPathXml.GetNodeAttr(node, "type");
                if (type != null)
                {
                    type = type.ToLower();
                    if (type.Equals("xsd:boolean"))
                    {
                        BooleanField f = new BooleanField(false);
                        this.dataDictionary.AddField(name, f);
                    }
                    if (type.Equals("xsd:string"))
                    {
                        StringField f = new StringField("");
                        this.dataDictionary.AddField(name, f);
                    }
                }
            }

            return true;
        }

        private bool ParseValue(XmlNode node)
        {
            String name = node.Name;
            Field f = this.dataDictionary.GetField(name);
            if (f == null)
            {
                return false;
            }
            if (f.GetFieldType() == FieldType.ComplexType)
            {
                return true;
            }
            String value = node.InnerText;
            if (value != null && value.Length > 0)
            {
                f.SetValue(value);
            }
            return true;
        }

    }
}
